/* This dofile is to "re-run" the randomization 
and calculate the potential share of mentees of each school 
for both grade 10 and grade 12 students 

for details,
see dofiles 1.1.1 and 1.2.1 for grade 10 
see dofiles 1.1.2 and 1.2.2 for grade 12 

Idea: identify the number of available mentors in each strata (fixed for all randomizations)

In each randomization trial, obtain the number of treated schools, based on which we compute the potential share such that the number of mentors is proportional to grade size 

After 1000 trials,  calculate the average potential share for each school 

Save these information, to be merged to the output from step dofiles 4.1.cleaning_gradeXX
*/

* generate value of random seeds 
drop _all
set obs 1000
set seed 142857 
g value=int(10^6*runiform())
qui levelsof value
gl randomseed "`r(levels)'"

********************************************************************************
*
*					GRADE 12
*
********************************************************************************

clear 
g schoolname_bl=""
g sharementee=.
save "$temp/sharementee.dta", replace emptyok

use "$temp/tvet_analysis.dta", clear
drop schoolname_b
drop if missing(schoolname_bl)

* number of available mentors 
scalar nmentor_tti_ks   	= 40 + 21
scalar nmentor_tti_thimphu 	= 26 + 23
scalar nmentor_tti_tyangste = 30

bys schoolname_bl: egen totstudent = total(~missing(uniqueid))

duplicates drop schoolname_bl, force 

* stratificatioon 
g group=1 if tti_ks_bl==1
replace group=2 if tti_thimphu_bl ==1
replace group=3 if tti_tyangste_bl ==1

* check if it is feasible to randomize with 
egen geopri = group(group private_bl)
tab geopri

levelsof geopri 
global geoprilist "`r(levels)'"
g groupsize=.
foreach i of global geoprilist {
	summ total1112_bl if geopri==`i', de
	local med`i' = r(p50)
	dis "`med`i''"
	replace groupsize = 1 if total1112_bl<=`med`i'' & geopri==`i'
	replace groupsize = 2 if total1112_bl>`med`i''  & geopri==`i'
}

egen geoprisize = group(group private_bl groupsize)
tab geoprisize
replace geoprisize = 13 if geoprisize==14


* randomization, stratified by geographic + private group + size
foreach j of global randomseed {
cap drop treat*
bys geoprisize: g rand_num`j'=runiform()
bys geoprisize: egen ordering`j'=rank(rand_num`j')
bys geoprisize: g treat_`j' = ordering`j'>((_N*3-4)/4)
drop rand_num`j' ordering`j'

cap drop sharementee = .
cap g sharementee = 0

foreach i in tti_ks tti_thimphu tti_tyangste {
	* total students in each "strata"
	egen totalgrade12_`i' = total(totstudent*(treat_`j'==1)*(`i'_bl==1)) 
	
	* number of mentees depends on number of available mentors & school size
	replace sharementee = 5*(int(`=scalar(nmentor_`i')'*(totstudent/totalgrade12_`i')))/totstudent if `i'_bl==1 & treat_`j'==1 
	
	cap drop totalgrade12_`i'
	preserve
	keep schoolname_bl sharementee geoprisize
	tempfile share`j' 
	save `share`j'', replace 
	restore 
}


preserve 
clear 
use "$temp/sharementee.dta"
append using `share`j''
save "$temp/sharementee.dta", replace 
restore 
}


use "$temp/sharementee.dta", clear
bys schoolname_bl: egen potentialshare = mean(sharementee)
duplicates drop schoolname_bl, force
drop sharementee
ren geoprisize strata
save "$clean/sharementee_grade12.dta", replace